// import parseTime, formatTime and set to filter
export { parseTime, formatTime } from '@/utils'

/**
 * Show plural label if time is plural number
 * @param {number} time
 * @param {string} label
 * @return {string}
 */
function pluralize(time, label) {
  if (time === 1) {
    return time + label
  }
  return time + label + 's'
}
/**
 * 时间
 * @param {*} time 
 */
export function timeAgo(time) {
  time = String(time).length === 10 ? time : (time / 1000)
  const between = Date.now() / 1000 - Number(time)
  if (between < 3600) {
    return pluralize(~~(between / 60), ' minute')
  } else if (between < 86400) {
    return pluralize(~~(between / 3600), ' hour')
  } else {
    return pluralize(~~(between / 86400), ' day')
  }
}

/**
 * Number formatting
 * like 10000 => 10k
 * @param {number} num
 * @param {number} digits
 */
export function numberFormatter(num, digits) {
  const si = [
    { value: 1E18, symbol: 'E' },
    { value: 1E15, symbol: 'P' },
    { value: 1E12, symbol: 'T' },
    { value: 1E9, symbol: 'G' },
    { value: 1E6, symbol: 'M' },
    { value: 1E3, symbol: 'k' }
  ]
  for (let i = 0; i < si.length; i++) {
    if (num >= si[i].value) {
      return (num / si[i].value).toFixed(digits).replace(/\.0+$|(\.[0-9]*[1-9])0+$/, '$1') + si[i].symbol
    }
  }
  return num.toString()
}

/**
 * 10000 => "10,000"
 * @param {number} num
 */
export function toThousandFilter(num) {
  return (+num || 0).toString().replace(/^-?\d+/g, m => m.replace(/(?=(?!\b)(\d{3})+$)/g, ','))
}

/**
 * Upper case first char
 * @param {String} string
 */
export function uppercaseFirst(string) {
  return string.charAt(0).toUpperCase() + string.slice(1)
}

/**
* [isDefault 默认值]
* @param  {[type]} content            [内容]
* @param  {String} [str='似乎没有内容'] [替换内容]
* @return {[type]}                    [description]
*/
export const isDefault = (content, str = '似乎没有内容') => {
  if (!content) {
    return str
  }
  return content
}

/**
* [empty 默认值]
* @param  {[type]} content            [内容]
* @param  {String} [str='似乎没有内容'] [替换内容]
* @return {[type]}                    [description]
*/
export const empty = (content, str = '似乎没有内容') => {
  if (!content) {
    return str
  }
  return content
}

/**
 * 截取数据
 * @param {*} content
 * @param {*} start
 * @param {*} length
 * @param {*} dot
 */
export const split = (content, start = 0, length = 10, dot = true) => {
  if (!content) {
    return ''
  }
  if (content.length <= length) {
    return content
  }
  return dot ? content.substr(start, length) + '...' : content.substr(start, length)
}
/**
 * 日期转换
 * @param {*} phptime 
 * @param {*} fmt 
 */
export const date_formart = (phptime, fmt = 'yyyy-MM-dd hh:mm:ss') => {
  phptime = String(phptime).length === 10 ? (phptime * 1000) : phptime
  var now = new Date(phptime)
  var o = {
    'M+': now.getMonth() + 1, // 月份
    'd+': now.getDate(), // 日
    'h+': now.getHours() % 12 === 0 ? 12 : now.getHours() % 12, // 小时
    'm+': now.getMinutes(), // 分
    's+': now.getSeconds(), // 秒
    'q+': Math.floor((now.getMonth() + 3) / 3), // 季度
    'S': now.getMilliseconds() // 毫秒
  }

  var week = {
    '0': '/u65e5',
    '1': '/u4e00',
    '2': '/u4e8c',
    '3': '/u4e09',
    '4': '/u56db',
    '5': '/u4e94',
    '6': '/u516d'
  }

  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(RegExp.$1, (now.getFullYear() + '').substr(4 - RegExp.$1.length))
  }
  if (/(E+)/.test(fmt)) {
    fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? '/u661f/u671f' : '/u5468') : '') +
      week[now.getDay() + ''])
  }
  for (var k in o) {
    if (new RegExp('(' + k + ')').test(fmt)) {
      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
    }
  }
  return fmt
}

/**
 * 数组过滤
 * @param {*} id 
 * @param {*} arr 
 */
export const array_filter = (id, arr = []) => {
  if (arr.length === 0) {
    return ''
  }
  const results = arr.find(item => {
    return Number(item.id) === Number(id)
  })
  if (!results) {
    return ''
  }
  return results.name
}

/**
 * 首字母大写
 * @param String str 字符串
 */
export const upperCase = (str) => {
  return str.toLowerCase().replace(/\b([\w|']+)\b/g, function(word) {
    return word.replace(word.charAt(0), word.charAt(0).toUpperCase())
  })
}
/**
 * \r\n2br
 * @param String str 字符串
 */
export const toBrLine = (str) => {
  if (str) {
    return str.replace(/\r\n/g, '<br>')
  } else {
    return ''
  }
}
